package com.shuai.mapper;


import com.github.pagehelper.Page;
import com.shuai.annotation.AutoFill;
import com.shuai.dto.UserPageQueryDTO;
import com.shuai.entity.User;
import com.shuai.enumeration.OperationType;
import com.shuai.vo.UserVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {

    /**
     * 根据用户名查询员工
     * @param username
     * @return
     */
    @Select("select * from user where username = #{username}")
    User getByUsername(String username);

    /**
     * 插入员工数据
     * @param user
     */


    @AutoFill(value = OperationType.INSERT)
    @Insert("insert into  user(name, username, password, phone , create_time, update_time)" +
            "value " +
            "(#{name}, #{username}, #{password}, #{phone} ,  #{createTime}, #{updateTime})")
    void insert(User user);


    /**
     * 分页查询
     * */
    Page<UserVO> pageQuery(UserPageQueryDTO userPageQueryDTO);

    /**
     *更改员工信息
     */
    @AutoFill(value = OperationType.UPDATE)
    void update(User user);

    /**
     *根据Id查询员工信息
     */
    @Select("select * from user where id = #{id}")
    User getById(Long id);


    /**
     * 根据id检查密码
     * @param userId
     * @param password
     * @return
     */
    @Select("select count(*) from user where id = #{userId} and password = #{password}")
    int checkPassword(Long userId, String password);

    /**
     *
     * @param userId
     * @param password
     */
    @Update("update user set password = #{password} where id = #{userId}")
    void updatePassword(Long userId, String password);

    @Select("select  id,username,name,phone,permission  from user where enterprise_id = #{enterpriseId}")
    List<UserVO> listAllUsersInEnterprise(Long enterpriseId);

    @Select("select id from user where username = #{username}")
    Long getUserIdByUsername(String username);

    @Select("select id,username,name,phone,permission from user where username like concat('%',#{username},'%') and enterprise_id != #{enterpriseId}")
    List<UserVO> listByUsername(String username , Long enterpriseId);
}
